package lesson4;

public class Demo4 {

    public static void main(String[] args) {
        int[] arr = {7798,22,45,12,67,89,123,12,56,88,99};
        //14
        //1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16
        //9,10,11,12,13,14,15,16
        //13,14,15,16
        //13,14
        //14
        int index = searchArr(arr,123);
        System.out.println(index);
        //排序算法
        //从小到大排序
        //int[] arr = {22,45,12,67,89,123,56,88,99,7798};
//        printArr(arr);
//        sort(arr);
//        printArr(arr);

    }

    static void printArr(int[] arr){
        for(int i = 0; i < arr.length; i++){
            System.out.print(arr[i]+"  ");
        }
        System.out.println();
    }

    static void sort(int[] arr){
        for(int i = 0; i < arr.length-1; i++){  //冒泡排序循环次数
            for(int k = 0; k < arr.length-1-i; k++){//每次循环下标的范围
                if(arr[k]>arr[k+1]){
                    //交换
                    int temp = arr[k];
                    arr[k] = arr[k+1];
                    arr[k+1] = temp;
                }
            }
        }
    }

    /**
     * 从数组arr中查找value元素
     * @param arr  要查找的数组
     * @param value 要查找的元素
     * @return  返回元素对应的下表，如果找不到返回-1
     */
    static int searchArr(int[] arr,int value){
        int index = 0;
        for(;index<arr.length; index++){
            if(arr[index] == value){
                return index;
            }
        }
        //遍历结束没找到
        if(index == arr.length){
            index = -1;
        }
        return index;

    }
}
